package brian.android.mvc.wiget;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class SelfListAdapter<T> extends ArrayAdapter<T>  {
	private SimpleListAdapter listAdapter;
	private int listItemView;
	
	public SelfListAdapter(Context context, int textViewResourceId, List<T> list) {
		super(context, textViewResourceId, list);
		
		if (!(context instanceof SimpleListAdapter)) {
			//TODO 여기서 Exception 처리 
			Log.e(this.getClass().getSimpleName(), context.getClass().getSimpleName() + " is not instance of " + SimpleListAdapter.class.getSimpleName());
		}
		this.listAdapter = (SimpleListAdapter) context;
		this.listItemView = textViewResourceId;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		T listItem = getItem(position);
		View listItemView = getListItemView(convertView);

		listAdapter.setListItemView(listItem, listItemView, position, convertView, parent);
		return listItemView;
	}

	private View getListItemView(View convertView) {
		View v = convertView;
		
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(listItemView, null);
		}
		return v;
	}
}
